cmake_minimum_required(VERSION 3.10)

project(qtv_mainframe VERSION 1.0 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


if (${QTV_ACTIVEX})
	find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Network AxServer REQUIRED)
	find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Network AxServer REQUIRED)
else()
	find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets Network REQUIRED)
	find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets Network REQUIRED)
endif()

set(PRJ_HEADERS
	osm_frame_widget.h
	osmtiles/cProjectionMercator.h
	osmtiles/tilesviewer.h
	osmtiles/urlDownloader.h
	osmtiles/layer_tiles.h
	osmtiles/layer_browser.h
	osmtiles/layer_interface.h
	osmtiles/viewer_interface.h
	osmtiles/layer_tiles_page.h
	interface_utils.h
)

set(PRJ_SOURCES
	osm_frame_widget.cpp
	osmtiles/cProjectionMercator.cpp
	osmtiles/tilesviewer.cpp
	osmtiles/urlDownloader.cpp
	osmtiles/layer_tiles.cpp
	osmtiles/layer_browser.cpp
	osmtiles/layer_tiles_page.cpp
)

set(PRJ_FORMS
	osm_frame_widget.ui
	osmtiles/layer_tiles_page.ui
)

set(PRJ_RESOURCES
	resource/resource.qrc
	qtviewer_planetosm_zh_CN.ts
	qtaxviewer_planetosm.rc
)

# ========================Exe========================
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
	qt_add_executable(qtviewer_planetosm
	MANUAL_FINALIZATION
	main.cpp
	listen_thread.cpp
	listen_thread.h
	${PRJ_HEADERS}
	${PRJ_SOURCES}
	${PRJ_FORMS}
	${PRJ_RESOURCES}
	)
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET qtviewer_planetosm APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
	if(ANDROID)
	add_library(qtviewer_planetosm SHARED
		main.cpp
		listen_thread.cpp
		listen_thread.h
		${PRJ_HEADERS}
		${PRJ_SOURCES}
		${PRJ_FORMS}
		${PRJ_RESOURCES}
	)
	# Define properties for Android with Qt 5 after find_package() calls as:
	#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
	add_executable(qtviewer_planetosm
	main.cpp
	listen_thread.cpp
	listen_thread.h
	${PRJ_HEADERS}
	${PRJ_SOURCES}
	${PRJ_FORMS}
	${PRJ_RESOURCES}
	)
endif()
endif()

target_link_libraries(qtviewer_planetosm PRIVATE
	Qt${QT_VERSION_MAJOR}::Widgets
	Qt${QT_VERSION_MAJOR}::Network)

set_target_properties(qtviewer_planetosm PROPERTIES
	MACOSX_BUNDLE_GUI_IDENTIFIER qtv.planetosm.viewer
	MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
	MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
	MACOSX_BUNDLE TRUE
	WIN32_EXECUTABLE TRUE
)

if(QT_VERSION_MAJOR EQUAL 6)
	qt_finalize_executable(qtviewer_planetosm)
endif()

# ========================Widget Library========================
add_library(qplanetosm_widget SHARED
	qtwidget_planetosm.h
	qtwidget_planetosm.cpp
	${PRJ_HEADERS}
	${PRJ_SOURCES}
	${PRJ_FORMS}
	${PRJ_RESOURCES}
)

target_compile_definitions(qplanetosm_widget PRIVATE PLANETOSM_EXPORT_DLL)
target_link_libraries(qplanetosm_widget PRIVATE
	Qt${QT_VERSION_MAJOR}::Widgets
	Qt${QT_VERSION_MAJOR}::Network
)


#==========================ActiveX==========================
if (WIN32 AND ${QTV_ACTIVEX})
	if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
	qt6_add_axserver_library(axplanetosm SHARED NO_AX_SERVER_REGISTRATION
		qtaxviewer_planetosm.def
		qtaxviewer_planetosm.h
		qtaxviewer_planetosm.cpp
		${PRJ_HEADERS}
		${PRJ_SOURCES}
		${PRJ_FORMS}
		${PRJ_RESOURCES}
	)
	add_custom_command(TARGET axplanetosm
			 POST_BUILD
			 COMMAND echo If you want to reg server, please set Qt BIN PATH first
			 COMMAND idc.exe \"$<TARGET_FILE:axplanetosm>\" /regserverperuser
			 #COMMAND regsvr32 \"$<TARGET_FILE:axplanetosm>\"
			 #COMMAND idc.exe \"$<TARGET_FILE:axplanetosm>\" /regserver
			 )
	else()
	add_library(axplanetosm SHARED
		qtaxviewer_planetosm.def
		qtaxviewer_planetosm.h
		qtaxviewer_planetosm.cpp
		${PRJ_HEADERS}
		${PRJ_SOURCES}
		${PRJ_FORMS}
		${PRJ_RESOURCES}
		# C:\Qt\6.2.4\msvc2019_64\bin\idc.exe D:/projects/build-qtv.planet-Qt62vc-Release/bin/axplanetosm.dll /regserver""
	)
	add_custom_command(TARGET axplanetosm
			 POST_BUILD
			 COMMAND idc.exe \"$<TARGET_FILE:axplanetosm>\" /idl \"$<TARGET_FILE:axplanetosm>.idl\" -version 1.0
			 COMMAND midl.exe \"$<TARGET_FILE:axplanetosm>.idl\" /nologo /tlb  \"$<TARGET_FILE:axplanetosm>.tlb\"
			 COMMAND idc.exe \"$<TARGET_FILE:axplanetosm>\" /tlb \"$<TARGET_FILE:axplanetosm>.tlb\"
			 COMMAND idc.exe \"$<TARGET_FILE:axplanetosm>\" /regserverperuser
			 #COMMAND idc.exe \"$<TARGET_FILE:axplanetosm>\" /regserver
			 )
endif()
target_link_libraries(axplanetosm PRIVATE
	Qt${QT_VERSION_MAJOR}::Widgets
	Qt${QT_VERSION_MAJOR}::Network
	Qt${QT_VERSION_MAJOR}::AxServer
)

endif()
